package com.nanbei.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nanbei.commonUntil.R;
import com.nanbei.eduService.client.VodClient;
import com.nanbei.eduService.entity.EduVideo;
import com.nanbei.eduService.mapper.EduVideoMapper;
import com.nanbei.eduService.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanbei.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-29
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    /**
     * 根据课程ID删除小节信息
     * @param courseId
     * TODO 需完善先删除小节视频
     */
    @Override
    public void delVideoByCourseById(String courseId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id" ,courseId);

        //查询出课程里面的所有小节
        List<EduVideo> eduVideos = baseMapper.selectList(queryWrapper);
        //定义一个数组保存所有小节里面的视频Id
        ArrayList<String> listId = new ArrayList<>();
        for (EduVideo eduVideo:eduVideos) {
            if (!StringUtils.isEmpty(eduVideo.getVideoSourceId())){
                listId.add(eduVideo.getVideoSourceId());
            }
        }

        //调用删除视频的接口
        if (listId.size()>0){
            R r = vodClient.delSomeVideos(listId);
            if (r.getCode() == 20001){
                throw new GuliException(20001,"视频删除失败，请检查删除视频服务");
            }
        }


        //删除小节
        this.baseMapper.delete(queryWrapper);
    }
}
